package fr.xgouchet.wedding.client.view;

import java.util.Date;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.i18n.Messages;
import fr.xgouchet.wedding.client.presenter.SettingsPresenter.Display;
import fr.xgouchet.wedding.shared.model.ProjectSettings;
import fr.xgouchet.wedding.shared.util.Encryption;

/**
 * UI Widget for Dashboard
 * 
 * @author x.gouchet
 * 
 */
public class SettingsView extends Composite implements Display {

	/**
	 * Default Constructor
	 */
	public SettingsView() {
		// create main panel
		VerticalPanel main = new VerticalPanel();
		initWidget(main);

		// create panels
		HorizontalPanel buttons = new HorizontalPanel();
		FlexTable form = new FlexTable();

		// create buttons
		mSaveButton = new Button(Constants.I18N.buttonSave());
		mCancelButton = new Button(Constants.I18N.buttonCancel());

		// create input widgets
		mWeddingDate = new DateBox();
		mWeddingDate.setValue(new Date());
		mWeddingDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(Constants.I18N.dateFormat())));
		mBrideFirstName = new TextBox();
		mGroomFirstName = new TextBox();
		mEmail = new TextBox();
		mChangePassword = new CheckBox();
		mCurrentPsswd = new PasswordTextBox();
		mCurrentPsswd.setEnabled(false);
		mNewPsswd = new PasswordTextBox();
		mNewPsswd.setEnabled(false);
		mNewPsswdCheck = new PasswordTextBox();
		mNewPsswdCheck.setEnabled(false);

		// create form
		form.setText(0, 0, Constants.I18N.labelWeddingDate());
		form.setWidget(0, 1, mWeddingDate);
		form.setText(1, 0, Constants.I18N.labelBrideFirstName());
		form.setWidget(1, 1, mBrideFirstName);
		form.setText(2, 0, Constants.I18N.labelGroomFirstName());
		form.setWidget(2, 1, mGroomFirstName);
		form.setText(3, 0, "E-mail");
		form.setWidget(3, 1, mEmail);
		form.setText(4, 0, Messages.I18N.changePassword());
		form.setWidget(4, 1, mChangePassword);
		form.setText(5, 0, Constants.I18N.labelPassword());
		form.setWidget(5, 1, mCurrentPsswd);
		form.setText(6, 0, Constants.I18N.labelPasswordNew());
		form.setWidget(6, 1, mNewPsswd);
		form.setText(7, 0, Constants.I18N.labelPasswordCheck());
		form.setWidget(7, 1, mNewPsswdCheck);

		// add local handlers
		mChangePassword
				.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
					public void onValueChange(ValueChangeEvent<Boolean> event) {
						mCurrentPsswd.setEnabled(event.getValue());
						mNewPsswd.setEnabled(event.getValue());
						mNewPsswdCheck.setEnabled(event.getValue());
					}
				});

		// final assembly
		buttons.add(mSaveButton);
		buttons.add(mCancelButton);
		main.add(form);
		main.add(buttons);
	}

	/**
	 * @see Display#getSaveButton()
	 */
	public HasClickHandlers getSaveButton() {
		return mSaveButton;
	}

	/**
	 * @see Display#getCancelButton()
	 */
	public HasClickHandlers getCancelButton() {
		return mCancelButton;
	}

	/**
	 * @see Display#displaySettings(ProjectSettings)
	 */
	public void displaySettings(ProjectSettings data) {
		mWeddingDate.setValue(data.getWeddingDate());
		mBrideFirstName.setValue(data.getBrideFirstName());
		mGroomFirstName.setValue(data.getGroomFirstName());
		mEmail.setValue(data.getUserMail());
	}

	/**
	 * @see Display#getSettingsValue()
	 */
	public ProjectSettings getSettingsValue() {
		ProjectSettings result = new ProjectSettings();
		result.setBrideFirstName(mBrideFirstName.getValue());
		result.setGroomFirstName(mGroomFirstName.getValue());
		result.setUserMail(mEmail.getValue());
		result.setWeddingDate(mWeddingDate.getValue());
		if (mChangePassword.getValue()) {
			result.setChangePassword(true);
			result.setCurrentPsswd(Encryption.md5(mCurrentPsswd.getValue()));
			result.setNewPsswd(Encryption.md5(mNewPsswd.getValue()));
			result.setNewPsswdCheck(Encryption.md5(mNewPsswdCheck.getValue()));
		}
		return result;
	}

	/**
	 * @see Display#asWidget()
	 */
	public Widget asWidget() {
		return this;
	}

	/** the Save button */
	private Button mSaveButton;

	/** the Cancel button */
	private Button mCancelButton;

	/** The Bride's First Name */
	private TextBox mBrideFirstName;

	/** The Groom's First Name */
	private TextBox mGroomFirstName;

	/** The wedding date */
	private DateBox mWeddingDate;

	/** The user's contact mail */
	private TextBox mEmail;

	/** change the password */
	private CheckBox mChangePassword;

	/** The user's current password */
	private PasswordTextBox mCurrentPsswd;

	/** The user's new password */
	private PasswordTextBox mNewPsswd;

	/** The user's confirm new password */
	private PasswordTextBox mNewPsswdCheck;
}
